package com.hospital.dictionary.module.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hospital.common.core.result.ErrorTip;
import com.hospital.common.core.result.SuccessTip;
import com.hospital.common.core.result.Tip;
import com.hospital.dictionary.config.DefaultMyBatisPlusConfig;
import com.hospital.dictionary.module.entity.ApiDictVsHis;
import com.hospital.dictionary.module.entity.SysDictItem;
import com.hospital.dictionary.module.service.ApiDictVsHisService;
import com.hospital.dictionary.module.service.SysDictItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

@RestController
@RequestMapping("/module/dictItem")
public class SysDictItemController {

    @Autowired
    private SysDictItemService sysDictItemService;

    @Autowired
    private ApiDictVsHisService apiDictVsHisService;

    /**
     * @功能：查询字典数据
     * @param sysDictItem
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Tip queryPageList(SysDictItem sysDictItem, @RequestParam(name = "tableName",defaultValue = "") String tableName, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                             @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req) {
        if(tableName!="") {
            DefaultMyBatisPlusConfig.TABLE_NAME.set(tableName);
            return new SuccessTip(sysDictItemService.list(new QueryWrapper<>()));
        }else {
            return new ErrorTip(302,"未传递表名");
        }
    }

    /**
     * @功能：新增
     * @return
     */
    //@RequiresRoles({"admin"})
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Tip add(@RequestBody SysDictItem sysDictItem) {
        try {
            DefaultMyBatisPlusConfig.TABLE_NAME.set(sysDictItem.getDictId());
            sysDictItemService.save(sysDictItem);
            ApiDictVsHis apiDictVsHis=new ApiDictVsHis();
            apiDictVsHis.setHisDictName(sysDictItem.getDictId());
            apiDictVsHis.setHisCode(sysDictItem.getCode());
            apiDictVsHis.setHisName(sysDictItem.getName());
            apiDictVsHis.setInsurDictName(sysDictItem.getDictId());
            apiDictVsHis.setInsurCode(sysDictItem.getCode());
            apiDictVsHis.setInsurName(sysDictItem.getName());
            int size = apiDictVsHisService.list(new QueryWrapper<ApiDictVsHis>()
                    .eq("HIS_DICT_NAME", apiDictVsHis.getHisDictName())
                    .eq("HIS_CODE", apiDictVsHis.getHisCode())
                    .eq("HIS_NAME",apiDictVsHis.getHisName())).size();
            if(size<=0){
                apiDictVsHisService.save(apiDictVsHis);
            }
            return new SuccessTip("操作成功");
        } catch (Exception e) {
            return new ErrorTip(302,"操作失败");
        }
    }

    /**
     * @功能：编辑
     * @param sysDictItem
     * @return
     */
    //@RequiresRoles({"admin"})
    @RequestMapping(value = "/edit", method = RequestMethod.PUT)
    public Tip edit(@RequestBody SysDictItem sysDictItem) {
        DefaultMyBatisPlusConfig.TABLE_NAME.set(sysDictItem.getDictId());
        SysDictItem sysdict = sysDictItemService.getById(sysDictItem.getSerialNo());
        if(sysdict==null) {
            return new ErrorTip(302,"操作失败");
        }else {
            boolean ok = sysDictItemService.updateById(sysDictItem);
            ApiDictVsHis apiDictVsHis=new ApiDictVsHis();
            apiDictVsHis.setHisDictName(sysDictItem.getDictId());
            apiDictVsHis.setHisCode(sysDictItem.getCode());
            apiDictVsHis.setHisName(sysDictItem.getName());
            apiDictVsHis.setInsurDictName(sysDictItem.getDictId());
            apiDictVsHis.setInsurCode(sysDictItem.getCode());
            apiDictVsHis.setInsurName(sysDictItem.getName());
            ApiDictVsHis one = apiDictVsHisService.getOne(new QueryWrapper<ApiDictVsHis>().eq("HIS_DICT_NAME", apiDictVsHis.getHisDictName()).eq("HIS_CODE", apiDictVsHis.getHisCode()));
            if(!one.getHisName().equals(sysDictItem.getName())){
                apiDictVsHis.setId(one.getId());
                apiDictVsHisService.updateById(apiDictVsHis);
            }

            //TODO 返回false说明什么？
            if(ok) {
                return new SuccessTip("操作成功");
            }
        }
        return new ErrorTip(302,"操作失败");
    }

    /**
     * @功能：删除字典数据
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public Tip delete(@RequestParam(name="id",required=true) String id, @RequestParam(name = "tableName",required = true) String tableName) {
        DefaultMyBatisPlusConfig.TABLE_NAME.set(tableName);
        SysDictItem joinSystem = sysDictItemService.getById(id);
        if(joinSystem==null) {
            return new ErrorTip(302,"操作失败");
        }else {
            boolean ok = sysDictItemService.removeById(id);
            ApiDictVsHis one = apiDictVsHisService.getOne(new QueryWrapper<ApiDictVsHis>().eq("HIS_DICT_NAME", tableName.toUpperCase()).eq("HIS_CODE", joinSystem.getCode()));
            if(one!=null){
                apiDictVsHisService.removeById(one.getId());
            }
            if(ok) {
               return new SuccessTip("操作成功");
            }
        }
        return new ErrorTip(302,"操作失败");
    }

    /**
     * @功能：批量删除字典数据
     * @param ids
     * @return
     */
    //@RequiresRoles({"admin"})
    @RequestMapping(value = "/deleteBatch", method = RequestMethod.DELETE)
    public Tip deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        if(ids==null || "".equals(ids.trim())) {
            return new ErrorTip(302,"参数不识别");
        }else {
            this.sysDictItemService.removeByIds(Arrays.asList(ids.split(",")));
            return new SuccessTip("操作成功");
        }
    }
}
